package com.abc.pro.model;

import java.util.ArrayList;

import android.util.Log;
import android.util.Pair;

public class GroupEvents<T extends Event>{
    public int sk;
    public String sn;
    public String tn;
    public long version;
    
    // private ArrayList<T> mEvents;
    private ArrayList<Pair<String, ArrayList<T>>> mEvents;
    public GroupEvents() {
        mEvents = new ArrayList<Pair<String, ArrayList<T>>>();
    }
    
    public Pair<String, ArrayList<T>> getEvent(int position){
        if(mEvents == null) return null;
        if(position < 0 || position >= mEvents.size()) return null;
        return mEvents.get(position);
    }
    
    public boolean addEvent(Pair<String, ArrayList<T>> event, int position){
        if(mEvents == null) return false;
        mEvents.add(position, event);
        return true;
    }
    
    public boolean addEvent(Pair<String, ArrayList<T>> event){
        if(mEvents == null) return false;
        return mEvents.add(event);
    }
    
    public int size(){
        return (mEvents == null) ? 0 : mEvents.size();
    }
    
    public ArrayList<OddRow> arrayFromMatch(T match){
        ArrayList<OddRow> result = new ArrayList<OddRow>();
        int count = match.getCount();
        OddRow row;
        for(int i = 0; i < count; i++){
            row = new OddRow();
            if(i == 0){
                row.setDate(match.getMatchDate());
                row.setTime(match.getMatchTime());
                row.setShowPlus(true);
                row.setShowStats(true);
            } else{//don't need to show date/time plus/stats
            	row.setShowPlus(false);
            	row.setShowStats(false);
            }
            
            match.updateRowData(row, i);
            
            result.add(row);
        }
        return result;
    }
    
    public ArrayList<Pair<String, ArrayList<OddRow>>> prepareData(){
        ArrayList<Pair<String, ArrayList<OddRow>>> result = new ArrayList<Pair<String,ArrayList<OddRow>>>();
        ArrayList<OddRow>temp;
        ArrayList<OddRow>temp2;
        for(Pair<String, ArrayList<T>> e: mEvents){
            temp = new ArrayList<OddRow>();
            for(T match : e.second){
                temp2 = arrayFromMatch(match);
                for(OddRow or : temp2){
                    temp.add(or);
                }
            }
            result.add(new Pair<String, ArrayList<OddRow>>(e.first, temp));
        }
        return result;
    }


    /*public ArrayList<OddRow> arrayFromMatch(T match){
        ArrayList<OddRow> result = new ArrayList<OddRow>();
        int count = match.getCount();
        OddRow row;
        for(int i = 0; i < count; i++){
            row = new OddRow();
            if(i == 0){
                row.setDate(match.getMatchDate());
                row.setTime(match.getMatchTime());
            }
            else{//don't need to show date/time
                
            }
            row.setHomeTeam(match.getHomeTeam());
            row.setAwayTeam(match.getAwayTeam());
            //todo: result
            
            if(match instanceof OddsEarlyEvent){
                OddsEarlyEvent e = (OddsEarlyEvent)match;
                EventType.O1x2[] o1x2 = e.get1x2();
                if(o1x2 != null && i < o1x2.length){
                    row.setFull1x2Result(o1x2[i].homeWinvalue);
                    row.setFull1x2Home(o1x2[i].awayWinValue);
                    row.setFull1x2Away(o1x2[i].drawValue);
                }
                EventType.Ah[] ah = e.getAh();
                if(ah != null && i < ah.length){
                    row.setFullhdp1(ah[i].homeValue.replace("-", "").replace("+", ""));
                    row.setFullhdp2_1(ah[i].homeOddsValue);
                    row.setFullhdp2_2(ah[i].awayOddsValue);
                }
                EventType.Ou[] ou = e.getOu();
                if(ou != null && i < ou.length){
                    row.setFullou1(ou[i].overValue.replace("-", "").replace("+", ""));
                    row.setFullou2_1(ou[i].overOddsValue);
                    row.setFullou2_2(ou[i].underOddsValue);
                }
                EventType.O1x21st[] o1x21st = e.get1x21st();
                if(o1x21st != null && i < o1x21st.length){
                    row.setHalf1x2Result(o1x21st[i].homeWinvalue);
                    row.setHalf1x2Home(o1x21st[i].awayWinValue);
                    row.setHalf1x2Away(o1x21st[i].drawValue);
                }
                EventType.Ah1st[] ah1st = e.getAh1st();
                if(ah1st != null && i < ah1st.length){
                    row.setHalfhdp1(ah1st[i].homeValue.replace("-", "").replace("+", ""));
                    row.setHalfhdp2_1(ah1st[i].homeOddsValue);
                    row.setHalfhdp2_2(ah1st[i].awayOddsValue);
                }
                EventType.Ou1st[] ou1st = e.getOu1st();
                if(ou1st != null && i < ou1st.length){
                    row.setHalfou1(ou1st[i].overValue.replace("-", "").replace("+", ""));
                    row.setHalfou2_1(ou1st[i].overOddsValue);
                    row.setHalfou2_2(ou1st[i].underOddsValue);
                }
                
            }
            
            result.add(row);
        }
        return result;
    }*/
}
